#pragma once

#include "bridge_motors_base.h"
#include "stdbool.h"

/*设备接口*/
typedef int (*device_pwm_init_fn_t)(int pwm_hz,int pwm_channel,int pwm_io);
typedef int (*device_pwm_set_fn_t)(int pwm_channel,int pwm_duty);
typedef int (*device_pwm_deinit_fn_t)(void);
struct device_pwm_i
{
    device_pwm_init_fn_t pwm_init;
    device_pwm_set_fn_t pwm_set;
    device_pwm_deinit_fn_t pwm_deinit;
};
int device_pwm_init(int pwm_hz,int pwm_channel,int pwm_io);
int device_pwm_set(int pwm_channel,int pwm_duty);
int device_pwm_deinit();

typedef int (*device_hb_gpio_init_fn_t)(int *io_num);
typedef int (*device_hb_gpio_set_fn_t)(int io_num, bool state);
struct device_hb_gpio_i
{
	device_hb_gpio_init_fn_t gpio_hb_init;
    device_hb_gpio_set_fn_t  gpio_hb_set;
};
int device_hb_gpio_init(int *io_num);
int device_hb_gpio_set(int io_num, bool state);

/*外调接口*/
/*pwm基本配置*/
struct bridge_motors_config{
    /*pwm配置*/
    int pwm_io1;
    int pwm_io2;
    int pwm_hz;

    /*io配置*/
    int io_sd1;
    int io_sd2;

    /*电机驱动配置*/
    int pwm_duty;
    int pwm_max_duty;
    int pwm_min_duty;
    int pwm_add_duty;
    int pwm_sub_duty;
};

typedef int (*enable_chip)(void *self);  //使能芯片
struct bridge_motors
{
    struct bridge_motors_i *interface;
    struct bridge_motors_config *config;
    struct device_pwm_i *device_pwm;
    struct device_hb_gpio_i *device_hb_gpio;
    enable_chip enable_chips;
    bool pwm_mutex_state;
    int io_list[2];
    bool direction;                     //电机转动方向
    bool start_flag;                    //电机启动标志
};
int bridge_motors_init(struct bridge_motors *self,struct bridge_motors_config *cfg);
int motors_correct(struct bridge_motors *self);
int motors_inverse(struct bridge_motors *self);
int motors_hasten(struct bridge_motors *self);
int motors_decelerate(struct bridge_motors *self);
int motors_stop(struct bridge_motors *self);

